package com.it.service.impl;
import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.it.constant.RedisConstant;
import com.it.dao.SetmealDao;
import com.it.entity.PageResult;
import com.it.entity.QueryPageBean;
import com.it.pojo.Setmeal;
import com.it.service.SetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import redis.clients.jedis.JedisPool;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 套餐管理服务
 */
@Service(interfaceClass = SetmealService.class)
@Transactional
public class SetmealServiceImpl implements SetmealService {

    @Autowired
    private SetmealDao setmealDao;
    @Autowired
    private JedisPool jedisPool;



    public PageResult pageQuery(QueryPageBean queryPageBean){



        Integer current = queryPageBean.getCurrentPage();
        Integer size = queryPageBean.getPageSize();
        String queryString = queryPageBean.getQueryString();
        PageHelper.startPage(current,size);
        Page<Setmeal> page = setmealDao.selectByCondition(queryString);

        return new PageResult(page.getTotal(),page.getResult());

    }

    public void add(Setmeal setmeal,Integer[] checkgroupIds){
        setmealDao.add(setmeal);
//        System.out.println("新增id:"+setmealId);
        if (checkgroupIds !=null && checkgroupIds.length >0){
            setSetmealAndCheckGroup(setmeal.getId(),checkgroupIds);

        }
        //将图片名称保存到Redis
//        savePic2Redis(setmeal.getImg());


    }

    public void setSetmealAndCheckGroup(Integer setmealId,Integer[] checkgroupIds){
        for (Integer checkgroupId:checkgroupIds) {
            Map<String,Integer> map = new HashMap<>();
            map.put("setmeal_id",setmealId);
            map.put("checkgroup_id",checkgroupId);
//            System.out.println("setmeal_id:%d checkgroup_id:%d",setmealId,checkgroupId);
            System.out.println("setmeal_id:"+setmealId +"  checkgroup_id:"+checkgroupId);
            System.out.println("**********************************************************");
            setmealDao.setSetmealAndCheckGroup(map);
        }

    }


    //将图片名称保存到Redis
    private void savePic2Redis(String pic){
        jedisPool.getResource().sadd(RedisConstant.SETMEAL_PIC_DB_RESOURCES,pic);
    }

    //删除套餐
    public void deleteById(Integer id){

        setmealDao.deleteSetmealAndCheckGroup(id);

        setmealDao.deleteById(id);
    }


    // 根据套餐id查询套餐信息
    public Setmeal findById(Integer id){
        Setmeal setmeal = setmealDao.findById(id);
        return setmeal;
    }

    //根据套餐id查询检查组列表
    public List<Integer> findGroupIdsBySetmealId(Integer id){
        List<Integer> groupIds = setmealDao.findGroupIdsBySetmealId(id);
        return  groupIds;

    }

    public List<Map<String, Object>> findSetmealCount() {
        return setmealDao.findSetmealCount();
    }
}
